package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.AliCode2;
import com.qian.api.ddj.model.StatusDict;
import com.qian.api.ddj.service.IAliCode2Service;
import com.qian.service.ddj.dao.AliCode2Repository;
import com.qian.service.ddj.entity.DdjAliCode2;
import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import com.yaowk.service.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.Date;

@Service
public class AliCode2ServiceImpl extends BaseServiceImpl<AliCode2, DdjAliCode2, Long> implements IAliCode2Service {

    @Autowired
    private AliCode2Repository dao;
    @Autowired
    private PageUtils pageUtils;

    @Override
    @Transactional

    public AliCode2 checkCode(Integer businessId, BigDecimal amount) {
        DdjAliCode2 ddjAliCode = dao.findFirstByBusinessIdAndAmountAndStatusOrderByVersion(businessId, amount, StatusDict.NORMAL);
        if (ddjAliCode != null) {
            ddjAliCode.setStatus(StatusDict.WAIT_PAY);
            ddjAliCode.setUseTime(new Date());
            dao.save(ddjAliCode);
        } else {
            return null;
        }
        return entity2model(ddjAliCode);
    }

    @Override
    @Transactional
    public void clear() {
        dao.clear();
    }

    @Override
    @Transactional
    public AliCode2 save(AliCode2 model) {
        if (model.isNew()) {
            DdjAliCode2 ddjAliCode = model2Entity(model);
            DdjAliCode2 ddjAliCode2 = dao.findFirstByBusinessIdAndAmountAndStatusOrderByVersion(model.getBusinessId(), model.getAmount(), StatusDict.NORMAL);
            if (isNotEmpty(ddjAliCode2))
                ddjAliCode.setVersion(ddjAliCode2.getVersion());

            model = entity2model(dao.save(ddjAliCode));
        } else {
            DdjAliCode2 ddjAliCode = dao.findOne(model.getId());
            if (isNotEmpty(model.getAmount()))
                ddjAliCode.setAmount(model.getAmount());
            if (isNotEmpty(model.getBusinessId()))
                ddjAliCode.setBusinessId(model.getBusinessId());
            if (isNotEmpty(model.getQrCode()))
                ddjAliCode.setQrCode(model.getQrCode());
            if (isNotEmpty(model.getRemark()))
                ddjAliCode.setRemark(model.getRemark());
            if (isNotEmpty(model.getStatus()))
                ddjAliCode.setStatus(model.getStatus());

            model = entity2model(dao.save(ddjAliCode));
        }
        return model;
    }

    @Override
    public PageResp<AliCode2> findAll(AliCode2 model, PageReq pageReq) {
        PageRequest pageRequest = pageUtils.pageReq2PageRequest(pageReq, new Sort(Sort.Direction.ASC, "amount"));
        Page<DdjAliCode2> page = dao.findAll(Example.of(model2Entity(model)), pageRequest);
        return page2pageResp(page);
    }

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected DdjAliCode2 model2Entity(AliCode2 model) {
        DdjAliCode2 ddjAliCode = null;
        if (isNotEmpty(model)) {
            ddjAliCode = new DdjAliCode2();
            ddjAliCode.setAmount(model.getAmount());
            ddjAliCode.setBusinessId(model.getBusinessId());
            ddjAliCode.setQrCode(model.getQrCode());
            ddjAliCode.setRemark(model.getRemark());
            ddjAliCode.setStatus(model.getStatus());
            ddjAliCode.setId(model.getId());
        }
        return ddjAliCode;
    }

    @Override
    protected AliCode2 entity2model(DdjAliCode2 entity) {
        AliCode2 aliCode = null;
        if (isNotEmpty(entity)) {
            aliCode = new AliCode2();
            aliCode.setAmount(entity.getAmount());
            aliCode.setBusinessId(entity.getBusinessId());
            aliCode.setQrCode(entity.getQrCode());
            aliCode.setRemark(entity.getRemark());
            aliCode.setStatus(entity.getStatus());
            aliCode.setId(entity.getId());
        }
        return aliCode;
    }
}
